package charles.testproject.testvalueandpointer;

public class TestValueAndPointer {
	public static void main(String[] args) {
		
		//第一个测试
		int x = 3;
		int y = 4;
		
		System.out.println("*******************Test_1*******************");
		
		System.out.println("origial x = " + x + "   y = " + y);
		swapInt(x,y);
		System.out.println("After swap x = " + x + "   y = " + y);
		
		//第二个测试
		
		System.out.println("*******************Test_2*******************");
		TestNode root = new TestNode();
		root.name = "string";
		TestNode pointer = null;
		//pointer 这一引用指向最开始创建的TestNode类型对象，现在root和pointer两个引用都指向
		//这一对象
		pointer = root;
		//用其中一个引用来修改对象的Name属性
		pointer.name = "pointer";
		System.out.println(root.name);
		root.name = "root-2";
		System.out.println(pointer.name);
		
		System.out.println("Are they == ?  " + (root == pointer));
		System.out.println("Are the euqals?  " + root.equals(pointer));
		
		//第三个测试
		
		//这个测试不知道是不是说明了in java ，all parameters to methods are passed by value?
		
		System.out.println("*******************Test_3*******************");
		
		
		TestNode node1 = new TestNode();
		node1.name = "node1";
		
		TestNode node2 = new TestNode();
		node2.name = "node2";
		
		System.out.println("Befor swapTestNode node1 = " + node1 + "   node2 = " + node2);
		System.out.println("Befor swapTestNode node1.name = " + node1.name + "   node2.name = " + node2.name);
		
		swapTestNode(node1,node2);
		
		System.out.println("After swapTestNode node1 = " + node1 + "   node2 = " + node2);
		System.out.println("After swapTestNode node1.name = " + node1.name + "   node2.name = " + node2.name);
	}

	private static void swapInt(int x, int y) {
		
		x = x + y;
		y = x - y;
		x = x - y;
		
		System.out.println("Inside swap x = " + x + "   y = " + y);
		
	}
	
	private static void swapTestNode(TestNode node1, TestNode node2) {
		TestNode tempNode = node1;
		node1 = node2;
		node2 = tempNode;
		
	}
	
}
